package com.example.personordersystem.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.R.integer;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.example.personordersystem.dao.FoodDao;
import com.example.personordersystem.dao.obj.Food;
import com.example.personordersystem.dbHelper.DBOpenHelper;

public class FoodDaoImpl implements FoodDao {
	private DBOpenHelper helper;
	private SQLiteDatabase db;

	public FoodDaoImpl(Context context) {
		context.deleteDatabase("data.db");
		helper = new DBOpenHelper(context);
	}

	private void getWritable() {
		db = helper.getWritableDatabase();

	}

	@Override
	public Food add(Food food) {
		getWritable();
		db.execSQL(
				"insert into t_Food(createdate,foodName,foodSupport,foodContent,hard) values(?,?,?,?,?)",
				new Object[] { food.getDate(), food.getFoodName(),
						food.getFoodSupport(), food.getFoodContent() });
		return food;

	}

	public void addTEST() {
		getWritable();
		db.execSQL(
				"insert into t_Food(createdate,foodName,foodSupport,foodContent,hard) values(?,?,?,?,?)",
				new Object[] { new Date(), "test", "123", "13123", "99" });
		db.execSQL(
				"insert into t_Food(createdate,foodName,foodSupport,foodContent,hard) values(?,?,?,?,?)",
				new Object[] { new Date(), "test2", "1234", "131234", "90" });
		db.execSQL(
				"insert into t_Food(createdate,foodName,foodSupport,foodContent,hard) values(?,?,?,?,?)",
				new Object[] { new Date(), "test2", "1234", "131234", "90" });
		db.execSQL(
				"insert into t_Food(createdate,foodName,foodSupport,foodContent,hard) values(?,?,?,?,?)",
				new Object[] { new Date(), "test2", "1234", "131234", "90" });
		db.execSQL(
				"insert into t_Food(createdate,foodName,foodSupport,foodContent,hard) values(?,?,?,?,?)",
				new Object[] { new Date(), "test2", "1234", "131234", "90" });
		db.execSQL(
				"insert into t_Food(createdate,foodName,foodSupport,foodContent,hard) values(?,?,?,?,?)",
				new Object[] { new Date(), "test2", "1234", "131234", "90" });
		db.execSQL(
				"insert into t_Food(createdate,foodName,foodSupport,foodContent,hard) values(?,?,?,?,?)",
				new Object[] { new Date(), "test2", "1234", "131234", "90" });
		db.execSQL(
				"insert into t_Food(createdate,foodName,foodSupport,foodContent,hard) values(?,?,?,?,?)",
				new Object[] { new Date(), "test2", "1234", "131234", "90" });
		db.execSQL(
				"insert into t_Food(createdate,foodName,foodSupport,foodContent,hard) values(?,?,?,?,?)",
				new Object[] { new Date(), "test2", "1234", "131234", "90" });
		db.execSQL(
				"insert into t_Food(createdate,foodName,foodSupport,foodContent,hard) values(?,?,?,?,?)",
				new Object[] { new Date(), "test2", "1234", "131234", "90" });
	}

	@Override
	public Food update(Food food) {

		return null;
	}

	@Override
	public Food find(int id) {

		return null;
	}

	@Override
	public int delete(Integer... ids) {

		return 0;
	}

	@Override
	public List<Food> getScrollData(int start, int end) {

		return null;
	}

	@Override
	public long count() {

		return 0;
	}

	@Override
	public int getcolumnCount() {
		getWritable();
		Cursor cursor = db.rawQuery("select * from t_Food limit 1,1", null);
		return cursor.getColumnCount() - 7;
	}

	@Override
	public List<String> getcolunmName() {
		getWritable();
		Cursor cursor = db.rawQuery("select * from t_Food limit 1,1", null);
		String[] strings = cursor.getColumnNames();
		List<String> list = new ArrayList<String>();
		for (int i = 7; i < strings.length; i++) {
			list.add(strings[i]);
		}
		return list;
	}

	/*
	 * public TestStudent add(TestStudent student) {
	 * 
	 * }
	 * 
	 * public TestStudent update(TestStudent student) { getWritable();
	 * db.execSQL( "update t_student set name=? and age=? where id=? ", new
	 * Object[] { student.getName(), student.getAge(), student.getId() });
	 * return student; }
	 * 
	 * public TestStudent find(int id) { getWritable(); Cursor cursor =
	 * db.rawQuery("select * from t_student where id=?", new String[] {
	 * String.valueOf(id) }); if (cursor.moveToNext()) { return new
	 * TestStudent(cursor.getInt(0), cursor.getString(1), cursor.getInt(2)); }
	 * return null; }
	 * 
	 * public int delete(Integer... ids) { if (ids.length > 0) { StringBuffer sb
	 * = new StringBuffer(); for (int i = 0; i < ids.length; i++) {
	 * sb.append("?").append(",");
	 * 
	 * } sb.deleteCharAt(sb.length() - 1); getWritable(); int i; return i =
	 * db.delete("t_student", "id in(" + sb + ")",
	 * ArrayToArray.intArrayToStringArray(ids));
	 * 
	 * } return 0; }
	 * 
	 * public List<TestStudent> getScrollData(int start, int end) {
	 * List<TestStudent> list = new ArrayList<TestStudent>(); getWritable();
	 * Cursor cursor = db.rawQuery("select * from t_student limit ?,?", new
	 * String[] { String.valueOf(start), String.valueOf(end) }); while
	 * (cursor.moveToNext()) { list.add(new TestStudent(cursor.getInt(0),
	 * cursor.getString(1), cursor.getInt(2))); }
	 * 
	 * return list;
	 * 
	 * }
	 * 
	 * public long count() { getWritable(); Cursor cursor =
	 * db.rawQuery("select count(id) from t_student", null); if
	 * (cursor.moveToNext()) { return cursor.getLong(0); } return 0;
	 * 
	 * }
	 */

	@Override
	public List<Map<String, Object>> getAllFood() {
		int count = 0;

		List<Map<String, Object>> map = new ArrayList<Map<String, Object>>();
		getWritable();
		Cursor cursor = db.rawQuery("select * from t_Food ", null);
		int olumnCount = cursor.getColumnCount();
		String[] strings = cursor.getColumnNames();
		while (cursor.moveToNext()) {
			HashMap<String, Object> hashMap = new HashMap<String, Object>();
			for (int i = 1; i <= olumnCount; i++) {
				if (i == 1) {
					hashMap.put(strings[i - 1], cursor.getInt(i - 1));
				} else {
					hashMap.put(strings[i - 1], cursor.getString(i - 1));
				}
			}
			map.add(hashMap);
		}

		return map;
	}
}
